package com.android.bolos;



import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;





public class Verconcursos extends ListActivity {

	private SQLiteDatabase baseDatos;
	TextView selection;
	private static final String TAG = "bdbolos";   
	private static final String nombreBD = "concursos";   
	private static final String creartablageneral = "create table if not exists "  
			  + " general (_id integer primary key autoincrement, "  
		  	  + " fecha text not null, " 
		  	  + " tiro1 text not null, tiro2 text not null, tiro3 text not null, tiro4 text not null, tiro5 text not null, tiro6 text not null, tiro7 text not null, tiro8 text not null, "
		  	  + " birle1 text not null, birle2 text not null, birle3 text not null, birle4 text not null, birle5 text not null, birle6 text not null, birle7 text not null, birle8 text not null, "
			  + " totalgeneral text not null, lugar text, tipo text, hora text );";
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.verconcursos);
   

  
	    // TODO Auto-enerated method stub
	    baseDatos = openOrCreateDatabase(Concurso.nombreBD, MODE_WORLD_READABLE, null); 
	    try 
	      {   
	        baseDatos = openOrCreateDatabase(nombreBD, MODE_WORLD_WRITEABLE, null);   
	        baseDatos.execSQL(creartablageneral);   
	      }    
	      catch (Exception e)
	      {   
	        Log.i(TAG, "Error al abrir o crear la base de datos" + e);   
	      }
		
     
   

		Cursor c = baseDatos.rawQuery("SELECT _id, fecha, totalgeneral, hora, tipo, lugar FROM general", null);
		
		String[] from = new String[] {"_id", "fecha", "hora", "totalgeneral", "tipo", "lugar"};
		
		int[] to = new int[] {R.id.codigo, R.id.fecha, R.id.hora, R.id.totalgeneral, R.id.tipo, R.id.lugar };
	
		
		// Now create an array adapter and set it to display using our row
		SimpleCursorAdapter concursos = new SimpleCursorAdapter(this, R.layout.lista, c, from, to);
		setListAdapter(concursos);
		

		    
	}

	public void onListItemClick(ListView l, View v, int position, long id) {
		 // TODO Auto-generated method stub
		 //super.onListItemClick(l, v, position, id);

		
		String keyword = "_id";
		Cursor c = (Cursor) getListAdapter().getItem(position);
		     int colIdx = c.getColumnIndex(keyword);
		        String identificador = c.getString(colIdx);
		 
		// iniciamos activity
		Intent envio=new Intent(Verconcursos.this ,Detalleconcurso.class);
		envio.putExtra("identificador", identificador);
		startActivity(envio);  
		 
		//Toast.makeText(this, "You selected: " + phone, Toast.LENGTH_LONG)	.show();
		//Cursor detalle = baseDatos.rawQuery("SELECT * FROM general WHERE _id = " + phone, null);
		 //String selection = l.getItemAtPosition(position).toString();
		 //Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
		//detalle.moveToFirst();
        //String identificador = detalle.getString(0);

		}	
	}



